package com.zpark.interecptor;

import com.zpark.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @PackageName: com.zpark.interecptor
 * @ClassName: UserIntercepter
 * @Description:
 * @author: RZS
 * @date: 2023/4/13  9:47
 */
@Component
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 判断用户是否登录，用户已经登录，则放行，否则拦截
         */
        HttpSession session = request.getSession();// 获取session
        User user = (User) session.getAttribute("user");// 获取session绑定的数据
        if (user == null) {
            // 用户没有登录，则转发到登录页面
            request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
            return false;
        }

        return true;
    }
}
